var CMD_TYPE_DATA = {
    "data": {
        "orderType": [
            {
                "name": "上行",
                "id": "up_cmd"
            },
            {
                "name": "下行",
                "id": "down_cmd"
            }
        ],
        "type": [
            {
                "name": "上线注册",
                "pid": "up_cmd",
                "id": "reg"
            }, {
                "name": "设备状态",
                "pid": "up_cmd",
                "id": "state"
            }, {
                "name": "配置报告",
                "pid": "up_cmd",
                "id": "conf"
            }, {
                "name": "执行投喂",
                "pid": "up_cmd",
                "id": "planfeed"
            }, {
                "name": "定量补投",
                "pid": "up_cmd",
                "id": "ext1feed"
            }, {
                "name": "定时补投",
                "pid": "up_cmd",
                "id": "ext2feed"
            }, {
                "name": "模式变更",
                "pid": "up_cmd",
                "id": "modechange"
            }, {
                "name": "饲料状态",
                "pid": "up_cmd",
                "id": "baitstatus"
            }, {
                "name": "停止上行",
                "pid": "up_cmd",
                "id": "stopstatus"
            }, {
                "name": "事件对账",
                "pid": "up_cmd",
                "id": "recon"
            }, {
                "name": "配置应答",
                "pid": "up_cmd",
                "id": "confack"
            }, {
                "name": "刷机模式应答",
                "pid": "up_cmd",
                "id": "firmwarerestartack"
            }, {
                "name": "刷机成功应答",
                "pid": "up_cmd",
                "id": "firmwaresendack"
            }, {
                "name": "注册应答",
                "pid": "down_cmd",
                "id": "regack"
            }, {
                "name": "读取状态",
                "pid": "down_cmd",
                "id": "readstate"
            }, {
                "name": "策略更新",
                "pid": "down_cmd",
                "id": "confupd"
            }, {
                "name": "自由投喂",
                "pid": "down_cmd",
                "id": "freefeed"
            }, {
                "name": "变更模式",
                "pid": "down_cmd",
                "id": "altmode"
            }, {
                "name": "饲料变更",
                "pid": "down_cmd",
                "id": "baitack"
            }, {
                "name": "对账召回",
                "pid": "down_cmd",
                "id": "callrecon"
            }, {
                "name": "设备重启",
                "pid": "down_cmd",
                "id": "reset"
            }, {
                "name": "更新时间",
                "pid": "down_cmd",
                "id": "updatetime"
            }, {
                "name": "进入刷机模式",
                "pid": "down_cmd",
                "id": "upgradestart"
            }, {
                "name": "发送刷机包",
                "pid": "down_cmd",
                "id": "upgradesend"
            },
        ]
    }
};
$(function () {
    var id = $.trim(MX.getQueryVariable("id"));
    if (id == null || id === "null" || !id) {
        LayerUtil.failMsg("未找到通讯详情，请重新进入")
        return false;
    }

    SelectCascade.builder()
        .data(CMD_TYPE_DATA.data)
        .dom($("#orderTypeId")).initByData()

    Request.builder()
        .url(STATION_NEW_DETAIL + "/" + id)
        .isToken(true)
        .callback(function (e) {
            if (e.code === "00000") {
                if (e.version == 3) {
                    $("#duizhang").show()
                }
                Form.setHtml($(".baseData"), e.data);
                var csq = e.data.csq;
                var color = csq > 18 ? '#2B99E6' : '#f8ac59';
                if (csq < 10) {
                    color = '#ED5565';
                }
                document.getElementById("signalNum").style.color = color;

                var csqChart = echarts.init(document.getElementById('csqChart'), null, {
                    renderer: 'canvas',
                    useDirtyRect: false
                });
                var csqOption = {
                    series: [
                        {
                            name: '信号强弱',
                            type: 'pie',
                            radius: ['40%', '70%'],
                            avoidLabelOverlap: false,
                            label: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                label: {
                                    show: true,
                                    fontSize: 20,
                                    fontWeight: 'bold'
                                }
                            },
                            labelLine: {
                                show: false
                            },
                            data: [
                                { value: 31, name: '离线', itemStyle: { color: 'rgba(208,208,208,0.8)' } },
                                { value: csq, name: '在线', itemStyle: { color: color } }
                            ]
                        }
                    ]
                };
                if (csqOption && typeof csqOption === 'object') {
                    csqChart.setOption(csqOption);
                }
                window.addEventListener('resize', csqChart.resize);
            } else if (e.code === "CSM08") {
                LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                setTimeout(function () {
                    window.location.href = "/admin/station/index.html";
                }, 1000)
            }
        }).get();

    //初始化统计图
    chart("", id);

    //报告次数
    $("#30TimeSearch").click(function () {
        var time = $.trim($("#30Time").val());
        chart(time, id);
    })
    //报告次数
    $("#30TimeReset").click(function () {
        $("#30Time").val("");
        chart("", id);
    })

    document.getElementById("30Time").value = DateUtil.changeDateToString(new Date().getTime(), DateFormatEnum.YYYY_MM);

    Table.builder()
        .listUrl(STATION_CMD_LIST + "?stationId=" + id)
        .init();

    $("#duizhang").click(function () {
        $("#dataTime").val("")
    });
    $("#zhuangtai").click(function () {
        operateStatus("确认读取状态?", id)
    });
    $("#chongqi").click(function () {
        operateReset("确认重启设备?", id)
    });

    $("#btn_upd_data").click(function () {
        var val = $("#dataTime").val()
        $("#dataDIV").find(".close").trigger("click");
        operateRecon("确认对账召回?", id, Date.parse(val))
    })
});

function chart(time, id) {
    Request.builder()
        .url(STATION_CMD_DAY_CHART)
        .data({"month": time, "stationId": id})
        .isToken(true)
        .callback(function (e) {
            var daylist = e.dayList;
            var numList = e.numList;
            var newDayList = [];
            var date = new Date();
            var yearStr = date.getFullYear() + "-";
            $.each(daylist, function (index, item) {
                item = item.replace(yearStr, "");
                newDayList.push(item)
            });
            var dom = document.getElementById('chart-vbar-1');
            var myChart = echarts.init(dom, null, {
                renderer: 'canvas',
                useDirtyRect: false
            });
            var option = {
                color: ["rgba(248,172,89,1)"],
                tooltip: {
                    trigger: "axis",
                    axisPointer: {
                        type: "shadow"
                    }
                },
                legend: {},
                grid: {left: '30', top: '30', right: '10', bottom: '25'},
                xAxis: {
                    type: 'category',
                    data: newDayList,
                    name: '',
                    axisTick: {
                        alignWithLabel: true
                    }
                },
                yAxis: {
                    type: 'value',
                    minInterval: 1
                },
                series: [
                    {
                        name: "通讯数据报告次数",
                        data: numList,
                        type: 'bar',
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(180, 180, 180, 0.2)'
                        }
                    }
                ]
            };
            if (option && typeof option === 'object') {
                myChart.setOption(option);
            }
            window.addEventListener('resize', myChart.resize);
        }).get();
}

//通用操作
function operateReset(msg, id) {
    var lock = false;
    var confirm = LayerUtil.confirm(msg, function () {
        if (!lock) {
            lock = true;
            Request.builder()
                .url(STATION_OPERATE_RESET + "/" + id)
                .isToken(true)
                .callback(function (e) {
                    if (e.code === "00000") {
                        LayerUtil.successMsg("指令已发出,请刷新页面查看结果~")
                    } else {
                        LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                    }
                }).post();
            layer.close(confirm);
        }
    }, null)
}
function operateStatus(msg, id) {
    var lock = false;
    var confirm = LayerUtil.confirm(msg, function () {
        if (!lock) {
            lock = true;
            Request.builder()
                .url(STATION_OPERATE_STATUS + "/" + id)
                .isToken(true)
                .callback(function (e) {
                    if (e.code === "00000") {
                        LayerUtil.successMsg("指令已发出,请刷新页面查看结果~")
                    } else {
                        LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                    }
                }).post()
            layer.close(confirm);
        }
    }, null)
}
function operateRecon(msg, id, time) {
    var lock = false;
    var confirm = LayerUtil.confirm(msg, function () {
        if (!lock) {
            lock = true;
            Request.builder()
                .url(STATION_OPERATE_RECON + "/" + id)
                .data({"day":time})
                .isToken(true)
                .callback(function (e) {
                    if (e.code === "00000") {
                        LayerUtil.successMsg("指令已发出,请刷新页面查看结果~")
                    } else {
                        LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                    }
                }).post()
            layer.close(confirm);
        }
    }, null)
}

columns = [
    {
        field: 'direction',
        title: '命令类型',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            if (value === 0) {
                var list = CMD_TYPE_DATA.data.type.filter(item=>item.pid === 'up_cmd');
                var info = list.find(item=> item.id === row.type)
                return `<span style='color:#00ffe7;padding-right: 6px;'>↑</span>${info.name}`;
            } else if (value === 1) {
                var list = CMD_TYPE_DATA.data.type.filter(item=>item.pid === 'down_cmd');
                var info = list.find(item=> item.id === row.type)
                return `<span style='color:red;padding-right: 6px;'>↓</span>${info.name}`;
            } else {
                return "<span style='color: #cccccc;padding-right: 6px;'>?</span>未知";
            }
        }
    }, {
        field: 'createTime',
        title: '上报时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD_HH_MM_SS);
        }
    }, {
        field: 'payload',
        title: 'PAYLOAD',
        align: 'center',
        valign: 'middle',
        formatter:function (value, row, index){
            value = JSON.parse(value);
            delete value.raw;
            return JSON.stringify(value);
        }
    }
]

